﻿using System;
using Rosetta.Default.Net;

namespace Rosetta.Default.AddIns.Net
{
    public sealed class WebAddressConverter : Converter
    {
        #region Constructors

        public WebAddressConverter()
            : base("WebAddress Converter")
        {
            Category = "Net";

            Add<string, WebAddress>("FromString", FromString);
            Add<string, string, WebAddress>("FromStringWithBaseUri", FromStringWithBaseUri, "baseUri", "relativeUri");
            Add<Uri, WebAddress>("FromUri", FromUri);
            Add<WebAddress, string>("ToString", ToString);
            Add<WebAddress, Uri>("ToUri", ToUri);
        }

        #endregion

        #region Class Methods

        private static WebAddress FromString([NotNull] string uriString)
        {
            return new WebAddress(new Uri(uriString));
        }

        private static WebAddress FromStringWithBaseUri([NotNull] string baseUri, [NotNull] string relativeUri)
        {
            return new WebAddress(new Uri(new Uri(baseUri), relativeUri));
        }

        private static WebAddress FromUri([NotNull] Uri uri)
        {
            return new WebAddress(uri);
        }

        private static string ToString(WebAddress address)
        {
            return address.Uri.AbsoluteUri;
        }

        private static Uri ToUri(WebAddress address)
        {
            return address.Uri;
        }

        #endregion
    }
}